#include"fileloader.h"
#include <QString>
#include <QIODevice>
#include <QFile>
#include <QImage>
#include <QVector>

FileLoader::FileLoader(){}

FileLoader * FileLoader::getInstance(){
    if(FileLoader::instance == NULL)
        FileLoader::instance = new FileLoader();
    return FileLoader::instance;
}

int FileLoader::loadFile(QString &sciezka, int sizeX, int sizeY, int sizeZ){
    if(FileLoader::instance == NULL)
        FileLoader::instance = new FileLoader();
    if((FileLoader::data) != NULL){
        for(unsigned int z=0;z<FileLoader::Z;z++){
            for(unsigned int y=0;y<FileLoader::Y;y++){
                delete [] (FileLoader::data)[z][y];    
            }
            delete [] (FileLoader::data)[z];
        }
        delete [] FileLoader::data;
        FileLoader::data=NULL;
    }
    QFile* file = new QFile(sciezka);
    int resault = file->open(QIODevice::ReadOnly);
    if(!resault){
        data = NULL;
        return -1;    
    }
    FileLoader::X=sizeX;
    FileLoader::Y=sizeY;
    FileLoader::Z=sizeZ;
    FileLoader::data = new unsigned int**[sizeZ];
    for(unsigned int z=0;z<FileLoader::Z;z++){
        (FileLoader::data)[z] = new unsigned int*[sizeY];
        for(unsigned int y=0;y<FileLoader::Y;y++){
            (FileLoader::data)[z][y] = new unsigned int[sizeX];   
        }
    }
    minValue = 64000;
    maxValue = 0;
    for(int z=0;z<sizeZ;z++){
        for(int y=0;y<sizeY;y++){
            for(int x=0;x<sizeX;x++){
                char data1 = 0, data2 = 0;
                if((file->read(&data1,1))!=1 || (file->read(&data2,1)!=1)){
                    for(unsigned int z=0;z<FileLoader::Z;z++){
                        for(unsigned int y=0;y<FileLoader::Y;y++){
                            delete [] (FileLoader::data)[z][y];    
                        }
                        delete [] (FileLoader::data)[z];
                    }
                    delete [] FileLoader::data;
                    FileLoader::data=NULL;
                    file->close();
                    delete(file);
                    return -1;
                }
                (FileLoader::data)[z][y][x] = (unsigned char)(data1)+(unsigned char)(data2)*(unsigned int)256;
                if((FileLoader::data)[z][y][x] > maxValue)
                    maxValue = (FileLoader::data)[z][y][x];
                if((FileLoader::data)[z][y][x] < minValue)
                    minValue = (FileLoader::data)[z][y][x];
            }    
        }    
    }
    file->close();
    delete(file);
    return 0;
}
    
unsigned int*** FileLoader::getDataPointer(){
    if(FileLoader::instance == NULL)
        FileLoader::instance = new FileLoader();
    return FileLoader::data;
}

int FileLoader::getXSize(){
    if(FileLoader::instance == NULL)
        FileLoader::instance = new FileLoader();
    if(FileLoader::data!=NULL)
        return FileLoader::X;
    else
        return 0;
}
    
int FileLoader::getYSize(){
    if(FileLoader::instance == NULL)
        FileLoader::instance = new FileLoader();
    if(FileLoader::data!=NULL)
        return FileLoader::Y;
    else
        return 0;
}
    
int FileLoader::getZSize(){
    if(FileLoader::instance == NULL)
        FileLoader::instance = new FileLoader();
    if(FileLoader::data!=NULL)
        return FileLoader::Z;
    else
        return 0;
}

QImage * FileLoader::getXImage(int x, int y, int z){
    unsigned int i,j;
    QImage * resault = new QImage(Z , Y, QImage::Format_Indexed8);
    QVector<uint> colorTable(256);
    for(i=0; i<256; i++)
        colorTable[i] = qRgb(i,i,i);
    colorTable[255] = qRgb(255,0,0);
    resault->setColorTable(colorTable);
    for(i=0; i<Z; i++){
        for(j=0; j<Y; j++){
            resault->setPixel(i, j, (unsigned int)((data[i][j][x-1]-minValue)*255.0/(maxValue-minValue)));
        }    
    }
    i=z; j=y; //for eliminate warnings 'unused paremeter'
    return resault;
}

QImage * FileLoader::getYImage(int x, int y, int z){
    unsigned int i,j;
    QImage * resault = new QImage(X , Z, QImage::Format_Indexed8);
    QVector<uint> colorTable(256);
    for(i=0; i<256; i++)
        colorTable[i] = qRgb(i,i,i);
    colorTable[255] = qRgb(255,0,0);
    resault->setColorTable(colorTable);
    for(i=0; i<X; i++){
        for(j=0; j<Z; j++){
            resault->setPixel(i, j, (unsigned int)((data[j][y-1][i]-minValue)*255.0/(maxValue-minValue)));
        }    
    }
    i=x; j=z; //for eliminate warnings 'unused paremeter'
    return resault;
}

QImage * FileLoader::getZImage(int x, int y, int z){
    unsigned int i,j;
    
    QImage * resault = new QImage(X , Y, QImage::Format_Indexed8);
    QVector<uint> colorTable(256);
    for(i=0; i<256; i++)
        colorTable[i] = qRgb(i,i,i);
    colorTable[255] = qRgb(255,0,0);
    resault->setColorTable(colorTable);
    for(i=0; i<X; i++){
        for(j=0; j<Y; j++){
            resault->setPixel(i, j, (unsigned int)((double)(data[z-1][j][i]-minValue)*255/(maxValue-minValue)));
        }    
    }
    i=x; j=y; //for eliminate warnings 'unused paremeter'
    return resault;
}

FileLoader* FileLoader::instance = NULL;
unsigned int FileLoader::count = 0;
unsigned int*** FileLoader::data = NULL;
unsigned int FileLoader::X=0;
unsigned int FileLoader::Y=0;
unsigned int FileLoader::Z=0;
unsigned int FileLoader::minValue=64000;
unsigned int FileLoader::maxValue=0;
